package eproject.mm.com.projectsupervision;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Vibrator;

//import com.baidu.mapapi.SDKInitializer;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import eproject.mm.com.projectsupervision.service.DemoService;
import eproject.mm.com.projectsupervision.service.KeepService;
import eproject.mm.com.projectsupervision.service.LocationService;
import eproject.mm.com.projectsupervision.service.StepService;
import eproject.mm.com.projectsupervision.util.ServiceUtil;
import eproject.mm.com.projectsupervision.util.TimeUtil;

/**
 * Created by momotea on 2018/3/16.
 */

public class PrjApplication extends Application {
    private static PrjApplication instance;
    private String authenticationToken;
    private String serverIp = "192.168.1.108";
    private String serverPort = "8080";
    private String preFixPath = "mobileService";
    private String userName;
    private String orgName;

    private String XGToken;

    public LocationService locationService;
    public Vibrator mVibrator;

    private String Process_Name = "eproject.mm.com.projectsupervision:localService";
    SharedPreferences userSettings;
    String workDate, workStatus;
    boolean isLogin = false;

    public static String LocalAction = "android.intent.action.MY_LOCAL_BROADCAST";
    public static String StepAction = "android.intent.action.MY_STEP_BROADCAST";
    public static String TabChangeAction = "android.intent.action.MY_TABCHANGE_BROADCAST";
    public static String RestStepCountAction = "android.intent.action.MY_RESTSETP_BROADCAST";
    public static String ServiceStartAction = "android.intent.action.MY_SERVICESTART_BROADCAST";
    public static String ServiceStopAction = "android.intent.action.MY_SERVICESTOP_BROADCAST";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        //PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,"XRQe8z8pwq5y6d9UMNTe59P5EaNjxdPh");

        initImageLoader(getApplicationContext());

        userSettings = getSharedPreferences("setting", 0);
        serverIp= userSettings.getString("serverip","");
        serverPort = userSettings.getString("serverport","");

        locationService = new LocationService(getApplicationContext());
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        //SDKInitializer.initialize(getApplicationContext());

        userSettings = getApplicationContext().getSharedPreferences("setting", 0);
        workDate = userSettings.getString("workdate","");
        workStatus = userSettings.getString("workstatus","0");


    }

    public String getAuthenticationToken() {
        //return "QzI3OEY2RDMwMzc5NDI4MUE3ODEzRERENjk2RjE5MkY=";
        if(authenticationToken==null){
            authenticationToken = "";
        }
        return authenticationToken;
    }

    public static PrjApplication getInstance(){
        // 因为我们程序运行后，Application是首先初始化的，如果在这里不用判断instance是否为空
        return instance;
    }
    public void setAuthenticationToken(String authenticationToken) {
        this.authenticationToken = authenticationToken;
    }

    public void setServerIp(String serverIp){
        getInstance().serverIp = serverIp;
    }
    public void setServerPort(String serverPort){
        getInstance().serverPort = serverPort;
    }
    public String getServerIp() {
        return serverIp;
    }

    public String getServerPort() {
        return serverPort;
    }

    public String getPreFixPath() {
        return preFixPath;
    }

    private void initImageLoader(Context context) {
        // TODO Auto-generated method stub
        // 创建DisplayImageOptions对象
        DisplayImageOptions defaulOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisk(true).build();
        // 创建ImageLoaderConfiguration对象
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
                context).defaultDisplayImageOptions(defaulOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        // ImageLoader对象的配置
        ImageLoader.getInstance().init(configuration);
    }

    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public void StartLocalService(int flag){
        Intent intent = new Intent(getApplicationContext(), DemoService.class);
        intent.putExtra("flag",flag);
        getApplicationContext().startService(intent);
    }
    public void StopLocalService(){
        Intent intent = new Intent(getApplicationContext(), DemoService.class);
        getApplicationContext().stopService(intent);
    }
    public boolean isServiceStarted(){
        boolean isRun = ServiceUtil.isProessRunning(getApplicationContext(), Process_Name);
        return isRun;
    }
    public void setLogin(boolean login){
        this.isLogin = login;
    }
    public boolean isLogin(){
        return isLogin;
    }

    public int getCurStepCount(){
        return userSettings.getInt("stepCount",0);
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public void setXGToken(String xgToken){
        XGToken = xgToken;
    }
    public String getXGToken(){
        return XGToken==null?"":XGToken;
    }
}
